package com.imedway.ihe.dd.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;


/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/31
 */
public class RegisterDocumentSetUtil {

    public static RegisterDocumentSet createRegisterDocumentSet() {
        Identifiable patientId = new Identifiable("P1", new AssigningAuthority("1.3.6.1.4.1.21367.2005.13.20.1000"));

        String targetId = UidUtil.uid();
        String sourceId = UidUtil.uid();

//        request.setSubmissionSet(createSubmissionSet(patientId));
        return RegisterDocumentSet
                .supportiveBuilderWith(createSubmissionSet(patientId, sourceId))
                .withDocuments(CollUtil.newArrayList(createDocumentEntry(patientId, targetId)))
                .withAssociation(createAssociation(targetId, sourceId))
                .build();
    }

    private static Association createAssociation(String targetId, String sourceId) {
        Association association = new Association();
        association.setEntryUuid(UidUtil.uid());
        association.setAssociationType(AssociationType.HAS_MEMBER);
        association.setSourceUuid(sourceId);
        association.setTargetUuid(targetId);
        association.setLabel(AssociationLabel.ORIGINAL);
        return association;
    }

    public static SubmissionSet createSubmissionSet(Identifiable patientId, String sourceId) {
        Recipient recipient = new Recipient();
        recipient.setOrganization(new Organization("org", null, null));
        SubmissionSet submissionSet = new SubmissionSet();

        submissionSet.setPatientId(patientId);
        submissionSet.setSourceId("1.2.3");
        submissionSet.setEntryUuid(sourceId);
        submissionSet.setLogicalUuid(sourceId);
        submissionSet.setAvailabilityStatus(AvailabilityStatus.APPROVED);
        submissionSet.setTitle(new LocalizedString("Physical"));

        submissionSet.setContentTypeCode(new Code("code1", new LocalizedString("code1"), "scheme1"));
        submissionSet.setSubmissionTime("20041225235051");
        submissionSet.setTitle(new LocalizedString("Physical"));
        submissionSet.setComments(new LocalizedString("Annual physical"));
        submissionSet.getAuthors().add(createAuthor());

        return submissionSet;
    }

    private static DocumentEntry createDocumentEntry(Identifiable patientId, String targetId) {
        DocumentEntry docEntry = new DocumentEntry();
        docEntry.setMimeType("text/plain");
        docEntry.setEntryUuid(targetId);
        docEntry.setLogicalUuid(targetId);
        docEntry.setAvailabilityStatus(AvailabilityStatus.APPROVED);

        // 设置文档类型
        docEntry.setType(DocumentEntryType.STABLE);
        
        docEntry.setCreationTime("20041225235052");
        docEntry.setHash(RandomUtil.randomNumbers(10));
        docEntry.setLanguageCode("zh");
        docEntry.setServiceStartTime("20041225235053");
        docEntry.setServiceStopTime("20041225235054");
        docEntry.setPatientId(patientId);
        docEntry.setSourcePatientId(patientId);
        docEntry.setSourcePatientInfo(createPatientInfo());
        docEntry.setRepositoryUniqueId("1.19.6.24.109.42.1.5");
        docEntry.setSize(100L);
        docEntry.setUri("https://www.example.com/documents/1");

        docEntry.setTitle(new LocalizedString("Document 01"));
        docEntry.getAuthors().add(createAuthor());
        docEntry.setClassCode(new Code("51851-4", new LocalizedString("Workflow"), "2.16.840.1.113883.6.1"));
        docEntry.setFormatCode(new Code("V", new LocalizedString("very restricted"), "2.16.840.1.113883.5.25"));
        docEntry.setHealthcareFacilityTypeCode(new Code("T-D4909", new LocalizedString("code4"), "scheme4"));
        docEntry.setPracticeSettingCode(new Code("urn:wustl:mir:xber:1999", new LocalizedString("code5"), "scheme5"));
        docEntry.setTypeCode(new Code("ACC", new LocalizedString("code6"), "scheme6"));
        docEntry.getConfidentialityCodes().add(new Code("394802001", new LocalizedString("code8"), "scheme8"));
        docEntry.getEventCodeList().add(new Code("34746-8", new LocalizedString("code9"), "scheme9"));
        return docEntry;
    }

    private static Author createAuthor() {
        Author author = new Author();
        author.setAuthorPerson(new Person(new Identifiable("1"), new XpnName()));
        author.getAuthorInstitution().add(new Organization("Cleveland Clinic", null, null));
        author.getAuthorInstitution().add(new Organization("Parma Community", null, null));
        author.getAuthorRole().add(new Identifiable("Attending"));
        author.getAuthorSpecialty().add(new Identifiable("Orthopedic"));
        return author;
    }

    private static PatientInfo createPatientInfo() {
        PatientInfo patientInfo = new PatientInfo();
        patientInfo.getNames().add(new XpnName("P1 patient", null, null, null, null, null));
        Address address = new Address();
        address.setCountry("China");
        patientInfo.getAddresses().add(address);
        patientInfo.setGender("M");
        patientInfo.setDateOfBirth("2000-12-01 00:00:00");
        return patientInfo;
    }
}
